#!/usr/bin/env python
# -*- coding:utf-8 -*-


"""
@Author : JoeyGosson
@Time   : 2019/10/08 14:40
@File   : resource_manager.py
"""

import os, sys, requests, time, random
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
from modules.log_model import create_logger
from fake_useragent import UserAgent as Ua
from modules.proxy_pool import get_proxy, hash_info, cron_run, get_proxy_random
from config.settings import *
from fake_useragent import UserAgent as ua


class Request_helper(object):
    def __init__(self):
        '''初始化'''
        self.log = create_logger(isPrint=PRINT_LOG)
        self.ua = ua()

    def request_robot(self, url, htmlname, trynum=1, timeout=30, headers=None, response_tyep="text", encoding_code=None, type_flag=False, type_flag2=False, type_flag3=False, check_flag=False, proxy_type=1):
        '''
        请求页面模板函数,详细的日志会打印在日志文件中(logs/log.log)
        :param htmlname:请求页面的标识名称
        :param trynum:尝试获取次数(默认重试一次)
        :param timeout:超时设置时间(默认30秒)
        :param proxies:设置代理信息(默认为空)
        :param response_tyep:请求成功返回的对象类型(默认为text)
        :return:返回一个相应对象
        '''
        try_num = 1
        while 1:
            try:
                if headers is None:
                    if "必发" in htmlname:
                        headers = {
                                "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
                                "User-Agent": Ua().random}
                    else:
                        headers = {"User-Agent": Ua().random}
                proxies = ""
                if proxy_type == 1:
                    ip = get_proxy(url)
                    if ip:
                        proxies = {"http:": ip}
                        headers['X-Forwarded-For'] = ip.split(":")[0]
                    else:
                        raise ValueError("get_proxy方法获取代理为空.")

                    if SHOW_PROXY:
                        self.log.info("本次使用的代理是:{}".format(proxies))

                if proxy_type == 2:
                    ip = get_proxy_random()
                    if ip:
                        proxies = {"http:": ip}
                        headers['X-Forwarded-For'] = ip.split(":")[0]
                    else:
                        raise ValueError("get_proxy方法获取代理为空.")
                    if SHOW_PROXY:
                        self.log.info("本次使用的代理是:{}".format(proxies))

                if type_flag:
                    url = url.split("version=")[0] + "version=" + time.strftime("%Y%m%d%H%M%S")
                if type_flag2:
                    url = url.split("version=")[0] + "version=" + time.strftime("%Y%m%d%H")

                response = requests.get(url, headers=headers, proxies=proxies, timeout=timeout)
                if response.status_code == 200:
                    if response_tyep == "text":
                        if encoding_code:
                            response.encoding = encoding_code
                        if check_flag:
                            if "频繁" in response.text:
                                raise ValueError("请求频繁")
                            else:
                                return response.text
                        else:
                            return response.text
                    return response
                if response.status_code == 443:
                    time.sleep(REQUEST_ERROR_SLEEP)
                    raise ValueError("响应状态码为:{}".format(response.status_code))
                raise ValueError("响应状态码为:{}".format(response.status_code))
            except BaseException as e:
                try_num += 1
                if try_num >= trynum:
                    raise ValueError("{}次请求{}失败,提示:{}".format(try_num, htmlname, str(e)[:200]))

